//
//  YTFollowAutherCell.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/26.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTFollowAutherCell.h"
@interface YTFollowAutherCell()
@property (weak, nonatomic) IBOutlet UIImageView *avatorImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UIButton *followButton;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;

@end
@implementation YTFollowAutherCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.coverImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick)];
    [self.coverImageView addGestureRecognizer:tap];
}

- (void)iconClick
{
    NSAssert(self.authorIconClickBlock != nil, @"authorIconClickBlock 不能为nil");
    
    self.authorIconClickBlock(self.isAuthorList ? self.model.uid : self.model.followuid);
}

- (void)setModel:(YTAuthorModel *)model
{
    _model = model;
    
    [self.avatorImageView sd_setImageWithURL:[NSURL URLWithString:model.user_avatar]];
    self.nameLabel.text = model.display_name;
    self.desLabel.text = model.des;
    self.followButton.selected = self.isAuthorList ? model.is_follow.boolValue : model.state.boolValue;
}

#pragma mark: - 关注按钮点击
- (IBAction)followButtonClick:(UIButton *)sender
{
    NSAssert(self.toFollowAuthorBlock != nil, @"toFollowAuthorBlock 不能为nil");
    if (!YTNetworkManager.shareManager.account.auth) {
        self.toFollowAuthorBlock(kShouldLoginCode);
        return;
    }
    
    [YTNetworkManager.shareManager requestFollowAuthorWithFollowID: self.isAuthorList ? self.model.uid : self.model.followuid
                                                          isFollow:sender.selected
                                                        completion:^(BOOL isSucc, BOOL isReward,NSString *error)
     {
         // 失败
         if (!isSucc) {
             [self.superview showTipWithTipStr:error];
             return ;
         }
         
        
         isReward ? [self showAwardWithTipStr:[NSString stringWithFormat:@"关注成功 +%@算力",error]] : [self showTipWithTipStr:error];
         sender.selected = !sender.selected;
         self.toFollowAuthorBlock(self.tag);
     }];
}

@end
